package com.insta.formation.library.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.insta.formation.library.Libraryfactory;
import com.insta.formation.library.Properties;
import com.insta.formation.library.dao.IBookDao;
import com.insta.formation.library.dao.JdbcTools;
import com.insta.formation.library.model.IBook;

public class BookDaoImpl implements IBookDao
{
	final private static Logger LOGGER = LoggerFactory.getLogger(BookDaoImpl.class);

	public IBook create(IBook book)
	{
		try {
			String query = "INSERT INTO " + Properties.BOOK_TABLE + " (isbn,name,author) VALUES (?,?,?)";
			Connection conn = JdbcTools.INSTANCE.getConnection();
			PreparedStatement stmt = conn.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);

			stmt.setString(1, book.getIsbn());
			stmt.setString(2, book.getName());
			stmt.setString(3, book.getAuthor());

			stmt.execute();
			ResultSet rs = stmt.getGeneratedKeys();
			if (rs.next()) {
				book = Libraryfactory.INSTANCE.createBook(rs.getBigDecimal(1).intValue(), book.getIsbn(), book.getName(), book.getAuthor());
			}
			stmt.close();
		} catch (SQLException e) {
			LOGGER.error(e.getMessage());
			book = null;
		}

		return book;
	}

	public IBook find(int id)
	{
		IBook book = null;
		try {
			String query = "SELECT * FROM " + Properties.BOOK_TABLE + " WHERE id=?";
			Connection conn = JdbcTools.INSTANCE.getConnection();
			PreparedStatement stmt = conn.prepareStatement(query);
			stmt.setInt(1, id);
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) {
				book = Libraryfactory.INSTANCE.createBook(rs.getInt("id"), rs.getString("isbn"), rs.getString("name"), rs.getString("author"));
			}
			stmt.close();

		} catch (SQLException e) {
			LOGGER.error(e.getMessage());
			book = null;
		}
		return book;
	}

	public IBook update(IBook book)
	{
		throw new UnsupportedOperationException();
	}

	public void delete(IBook o)
	{
		try {
			String query = "DELETE FROM " + Properties.BOOK_TABLE + " WHERE id=?";
			Connection conn = JdbcTools.INSTANCE.getConnection();
			PreparedStatement stmt = conn.prepareStatement(query);
			stmt.setInt(1, o.getId());
			stmt.execute();
			stmt.close();

		} catch (SQLException e) {
			LOGGER.error(e.getMessage());
			// e.printStackTrace();
			o = null;
		}

	}

	public IBook find(String isbn)
	{
		IBook book = null;
		try {
			String query = "SELECT * FROM " + Properties.BOOK_TABLE + " WHERE isbn=?";
			Connection conn = JdbcTools.INSTANCE.getConnection();
			PreparedStatement stmt = conn.prepareStatement(query);
			stmt.setString(1, isbn);
			ResultSet rs = stmt.executeQuery();
			if (rs.next()) {
				book = Libraryfactory.INSTANCE.createBook(rs.getInt("id"), rs.getString("isbn"), rs.getString("name"), rs.getString("author"));
			}
			stmt.close();

		} catch (SQLException e) {
			LOGGER.error(e.getMessage());
			book = null;
		}
		return book;
	}

}
